/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DetailedCriminal.java
 *
 * Created on Aug 26, 2011, 5:34:10 PM
 */
package Views;

import Controllers.CriminalController;
import Controllers.PrisonerController;
import Models.Criminal;
import Models.Prisoner;
import Models.Wanted;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Pan
 */
public class EditPrisoner extends javax.swing.JDialog {
    
    int _prisonerID;

    /** Creates new form DetailedCriminal */
    public EditPrisoner(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
    }
    
    public EditPrisoner(java.awt.Frame parent, boolean modal, Prisoner prisoner) {
        super(parent, modal);
        initComponents();
        
        _prisonerID = prisoner.getID();
        txtCriminalID.setText(String.valueOf(_prisonerID));
        txtPrisonName.setText(prisoner.getPrisonName());
        txtCellNumber.setText(prisoner.getCellNumber());
        spnImprisonDate.setValue(prisoner.getImprisonDate());
        if(prisoner.getReleaseDate()!=null){
            chxHasReleaseDate.setSelected(true);
            spnReleaseDate.setEnabled(true);
            spnReleaseDate.setValue(prisoner.getReleaseDate());
        }
        chxIsReleased.setSelected(prisoner.isIsReleased());
        txtReleaseReason.setText(prisoner.getReleaseReason());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btn_Close = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btn_Submit = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtPrisonName = new javax.swing.JTextField();
        txtCellNumber = new javax.swing.JTextField();
        spnImprisonDate = new javax.swing.JSpinner();
        spnReleaseDate = new javax.swing.JSpinner();
        chxHasReleaseDate = new javax.swing.JCheckBox();
        txtCriminalID = new javax.swing.JTextField();
        chxIsReleased = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtReleaseReason = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        jPanel1.setPreferredSize(new java.awt.Dimension(613, 50));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Prisoner Update Form");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(220, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btn_Close.setText("Close");
        btn_Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CloseActionPerformed(evt);
            }
        });

        jLabel2.setForeground(new java.awt.Color(255, 51, 51));
        jLabel2.setText("Imprisoned Criminal ID:");

        jLabel3.setForeground(new java.awt.Color(255, 51, 51));
        jLabel3.setText("Imprison Date:");

        jLabel5.setText("Release Date:");

        jLabel7.setForeground(new java.awt.Color(255, 51, 51));
        jLabel7.setText("Prison Name:");

        jLabel9.setForeground(new java.awt.Color(255, 51, 51));
        jLabel9.setText("Cell Plate:");

        btn_Submit.setText("Submit");
        btn_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SubmitActionPerformed(evt);
            }
        });

        jLabel4.setForeground(new java.awt.Color(255, 51, 51));
        jLabel4.setText("Red Labels: Mandatory fields");

        spnImprisonDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(-946796400000L), new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));
        spnImprisonDate.setEditor(new javax.swing.JSpinner.DateEditor(spnImprisonDate, "MM/dd/yyyy"));
        spnImprisonDate.setEnabled(false);

        spnReleaseDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(-946796400000L), new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));
        spnReleaseDate.setEditor(new javax.swing.JSpinner.DateEditor(spnReleaseDate, "MM/dd/yyyy"));
        spnReleaseDate.setEnabled(false);

        chxHasReleaseDate.setText("This prisoner has a release date");
        chxHasReleaseDate.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chxHasReleaseDateItemStateChanged(evt);
            }
        });
        chxHasReleaseDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chxHasReleaseDateActionPerformed(evt);
            }
        });

        txtCriminalID.setEditable(false);

        chxIsReleased.setText("This prisoner has been released");
        chxIsReleased.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chxIsReleasedItemStateChanged(evt);
            }
        });

        jLabel6.setText("Release Reason:");

        txtReleaseReason.setColumns(20);
        txtReleaseReason.setEditable(false);
        txtReleaseReason.setLineWrap(true);
        txtReleaseReason.setRows(5);
        jScrollPane1.setViewportView(txtReleaseReason);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 166, Short.MAX_VALUE)
                .addComponent(btn_Submit)
                .addGap(18, 18, 18)
                .addComponent(btn_Close)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtCriminalID))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addGap(4, 4, 4)
                            .addComponent(txtPrisonName, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(4, 4, 4)
                        .addComponent(txtCellNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spnImprisonDate, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chxIsReleased))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chxHasReleaseDate)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(4, 4, 4)
                        .addComponent(spnReleaseDate, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtCriminalID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(spnImprisonDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPrisonName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(chxIsReleased)))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel9))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCellNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(chxHasReleaseDate)
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel5))
                            .addComponent(spnReleaseDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Close)
                    .addComponent(btn_Submit)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_CloseActionPerformed
    
private void chxHasReleaseDateItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chxHasReleaseDateItemStateChanged
    // TODO add your handling code here:
}//GEN-LAST:event_chxHasReleaseDateItemStateChanged
    
private void chxHasReleaseDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chxHasReleaseDateActionPerformed
    spnReleaseDate.setEnabled(chxHasReleaseDate.isSelected());
}//GEN-LAST:event_chxHasReleaseDateActionPerformed
    
private void btn_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SubmitActionPerformed
    Prisoner _prisoner = new Prisoner();
    SimpleDateFormat formatter = new SimpleDateFormat("DD/mm/yyyy");

    _prisoner.setID(_prisonerID);
    _prisoner.setPrisonName(txtPrisonName.getText());
    _prisoner.setCellNumber(txtCellNumber.getText());
    if (chxHasReleaseDate.isSelected()) {
        String dateString = formatter.format(spnReleaseDate.getValue());
        try {
            _prisoner.setReleaseDate(formatter.parse(dateString));
        } catch (ParseException ex) {
            Logger.getLogger(EditPrisoner.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        _prisoner.setReleaseDate(null);
    }
    _prisoner.setIsReleased(chxIsReleased.isSelected());
    if(chxIsReleased.isSelected()){
        _prisoner.setReleaseReason(txtReleaseReason.getText().trim());
    }
    if (PrisonerController.EditPrisoner(_prisoner)) {
        JOptionPane.showMessageDialog(null, "Prisoner updated.");
    } else {
        JOptionPane.showMessageDialog(null, "Submission failed.");
    }
    this.dispose();
}//GEN-LAST:event_btn_SubmitActionPerformed
    
private void chxIsReleasedItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chxIsReleasedItemStateChanged
    txtReleaseReason.setEditable(chxIsReleased.isSelected());
}//GEN-LAST:event_chxIsReleasedItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                EditPrisoner dialog = new EditPrisoner(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Close;
    private javax.swing.JButton btn_Submit;
    private javax.swing.JCheckBox chxHasReleaseDate;
    private javax.swing.JCheckBox chxIsReleased;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnImprisonDate;
    private javax.swing.JSpinner spnReleaseDate;
    private javax.swing.JTextField txtCellNumber;
    private javax.swing.JTextField txtCriminalID;
    private javax.swing.JTextField txtPrisonName;
    private javax.swing.JTextArea txtReleaseReason;
    // End of variables declaration//GEN-END:variables
}
